﻿using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using TotalApplication.Core;
using TotalApplication.viewmodels.Video;
using TotalApplication.viewmodels.Video.Controls;

namespace TotalApplication.Mine_Video
{
	public static class VideoPathHelpers
	{
		public static string APP_DATA = @"D:\TZJ001\Dance\Data\";

		public static string TAG_PATH = APP_DATA + "Tags.json";

        public static Dictionary<string, TagLIstVM> VideoTagIndexDict = new Dictionary<string, TagLIstVM>();	

        public static void LoadTags()
		{
			try
			{
                if (File.Exists(TAG_PATH))
                {
                    TagItemOB? tagItemOB = JsonConvert.DeserializeObject<TagItemOB>(File.ReadAllText(TAG_PATH));
                    if (tagItemOB != null)
                    {
                        IoC.Kernel.Bind<TagsVM>().ToConstant(new TagsVM(tagItemOB));
                    }
                    else
                    {
                        IoC.Kernel.Bind<TagsVM>().ToConstant(new TagsVM());
                    }
                }
                else
                {
                    Directory.CreateDirectory(APP_DATA);
                    File.Create(TAG_PATH).Close();
                    IoC.Kernel.Bind<TagsVM>().ToConstant(new TagsVM());
                }
            }
			catch (Exception)
			{
                Directory.CreateDirectory(APP_DATA);
                File.Create(TAG_PATH).Close();
                IoC.Kernel.Bind<TagsVM>().ToConstant(new TagsVM());
            }
			
        }

        public static void SaveTags(TagsVM tagsVM)
		{
			try
			{
                var settings = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore, // 忽略循环引用
                    Formatting = Formatting.Indented
                };
                File.WriteAllText(TAG_PATH, JsonConvert.SerializeObject(new TagItemOB(tagsVM),settings));
                // File.WriteAllText(TAG_PATH, JsonConvert.SerializeObject(IoC.Get<TagsVM>()));
            }
            catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
            }
		}

		public static string GetHash(this TagLIstVM tagLIstVM)
		{
			return tagLIstVM.GetHashCode().ToString();
        }
    }
}
